using UnityEngine;

namespace PathX.Unity
{
    /// <summary>
    /// Monobehaviour单例
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public abstract class SingleInstanceComponent<T> : MonoBehaviour where T : MonoBehaviour
    {
        private static int _instanceMark = 0;
        private void Awake()
        {
            if (SingleInstanceComponent<T>._instanceMark > 0)
            {
                Object.Destroy(this);
            }
            else
            {
                SingleInstanceComponent<T>._instanceMark = 1;
                this.OnAwake();
            }
        }
        private void OnDestroy()
        {
            SingleInstanceComponent<T>._instanceMark = 0;
            OnDispose();
        }

        protected virtual void OnAwake()
        {
            
        }

        protected virtual void OnDispose()
        {
            
        }
    }
}